package com.ruoyi.framework.config;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.utils.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class LogcostInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(LogcostInterceptor.class);

    @Autowired
    private TokenUtils tokenUtils;

    //preHandle是在请求执行前执行的
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){

       // CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
       // if (StringUtils.isNotNull(customerLogin)){
            //logger.info("+++++++++++++++++++++++redis未过期,customerLogin :"+ customerLogin);
            // 刷新token失效
            //tokenUtils.verifyToken(customerLogin);
            return true;
        //}
       // logger.info("----------------token过期---------------");
       // return false;
        //返回true,postHandler和afterCompletion方法才能执行
        // 否则false为拒绝执行，起到拦截器控制作用
    }

    //postHandler是在请求结束之后,视图渲染之前执行的,但只有preHandle方法返回true的时候才会执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        System.out.println("Interception cost="+(System.currentTimeMillis()));
    }

    //afterCompletion是视图渲染完成之后才执行,同样需要preHandle返回true，
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        //该方法通常用于清理资源等工作
    }

}
